﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;
using System.Web.Script.Serialization;
using System.Web.WebPages;

namespace MvcToolbox
{
    public static class HtmlHelperExtensions
    {
        /// <summary>
        /// Returns a description for this model property
        /// </summary>
        public static IHtmlString DescriptionFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
        {
            Ensure.Argument.NotNull(html, "html");
            Ensure.Argument.NotNull(expression, "expression");
            
            var metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
            return new HtmlString(metadata.Description);
        }

        /// <summary>
        /// A helper for performing conditional IF,ELSE logic using Razor
        /// </summary>
        public static string IfElse(this HtmlHelper html, bool criteria, string trueString, string falseString)
        {
            Ensure.Argument.NotNull(html, "html");
            StringBuilder sb = new StringBuilder();
            if (criteria)
            {
                sb.Append(trueString);
            }
            else
            {
                sb.Append(falseString);
            }
            return sb.ToString();
        }

        public static IHtmlString If(this HtmlHelper html, bool criteria, Func<bool, HelperResult> render)
        {
            Ensure.Argument.NotNull(html, "html");
            Ensure.Argument.NotNull(render, "render");
            if (criteria)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append(render(criteria).ToHtmlString());
                return new HtmlString(sb.ToString());
            }
            return new HtmlString(string.Empty);
        }

        public static IHtmlString IfElse(this HtmlHelper html, bool criteria, Func<bool, HelperResult> render, Func<bool, HelperResult> renderAlt)
        {
            Ensure.Argument.NotNull(html, "html");
            Ensure.Argument.NotNull(render, "render");
            Ensure.Argument.NotNull(renderAlt, "renderAlt");
            StringBuilder sb = new StringBuilder();
            if (criteria)
            {
                sb.Append(render(criteria).ToHtmlString());
            }
            else
            {
                sb.Append(renderAlt(criteria).ToHtmlString());
            }
            return new HtmlString(sb.ToString());
        }

        public static IHtmlString Foreach<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, List<TProperty>>> expression, Func<int, HelperResult> render)
        {
            Ensure.Argument.NotNull(expression, "expression");
            Ensure.Argument.NotNull(render, "render");
            StringBuilder sb = new StringBuilder();
            ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
            List<TProperty> values = metadata.Model as List<TProperty>;
            int i = 0;
            foreach (var val in values)
            {
                sb.Append(render(i).ToHtmlString());
            }
            return new HtmlString(sb.ToString());
        }
    }
}
